package com.etonedu.meals.base;

import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentTransaction;

import com.etonedu.meals.R;
import com.etonedu.meals.common.SPConst;
import com.etonedu.meals.ui.fragment.AddQRFragment;
import com.etonedu.meals.ui.fragment.TestFragment;
import com.etonedu.meals.utils.SharedPreferencesUtil;


/**
 * Created by yf7429 on 2016/11/7.
 */

public class SystemBaseFragment extends Fragment {

    private static View view;
    public static SystemBaseFragment instance;
    private SharedPreferencesUtil spu;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = LayoutInflater.from(getActivity()).inflate(R.layout.fragment_system_base, null);
        spu = SharedPreferencesUtil.getInstance(getActivity());
        instance = this;
        initDefaultFragment();
        /*if(spu.getInt(SPConst.EVALEDU_FLAG) == 1){
            initWebSocket();
        }*/
        return view;
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);

    }

    @Override
    public void onDetach() {
        super.onDetach();

    }

    BaseFragment topFragment = null;
    BaseFragment fragment = null;
    BaseFragment bottomFragment = null;
    private void initDefaultFragment() {

        String templeCode = spu.getString(SPConst.TEMPLATE_CODE);

//        templeCode = "ECBS1001";
        // ECBS1001	学校模板 ;
        //	ECBC1001	班级模板;
        //	ECBR1001	宿舍模板;
        //	ECBA1001	学校区域模板

        if ("QR1001".equals(templeCode)) {
            fragment = new AddQRFragment();
        }

        setAndShowTopFragment(topFragment,null);
        setAndShowContentFragment(fragment,null);
        setAndShowBottomFragment(bottomFragment,null);
    }

    public void setAndShowContentFragment(Fragment fragment, Bundle bundle) {
        if (view == null)
            return;
        if (bundle != null)
            fragment.setArguments(bundle);
        addFragment(R.id.fl_content, fragment);
    }

    public void setAndShowTopFragment(BaseFragment fragment, Bundle bundle) {
        if (view == null)
            return;
        if (bundle != null)
            fragment.setArguments(bundle);
        addFragment(R.id.fl_top, fragment);
    }

    public void setAndShowBottomFragment(BaseFragment fragment, Bundle bundle) {
        if (view == null)
            return;
        if (bundle != null)
            fragment.setArguments(bundle);
        addFragment(R.id.fl_leave, fragment);
    }

    private void addFragment(int layoutID, Fragment baseFragment) {
        if (view == null)
            return;
        if (baseFragment == null) {
            view.findViewById(layoutID).setVisibility(View.GONE);
        } else {
            try {
//                FragmentTransaction fragmentTransaction = getFragmentManager()
//                        .beginTransaction();

                FragmentTransaction fragmentTransaction = getChildFragmentManager()
                        .beginTransaction();
                try {

                    fragmentTransaction.add(layoutID, baseFragment, baseFragment
                            .getClass().toString());
                } catch (Exception e) {
                    e.printStackTrace();
                }
                fragmentTransaction.setCustomAnimations(android.R.anim.fade_in,
                        android.R.anim.fade_out);
                fragmentTransaction.addToBackStack(null);
                fragmentTransaction.commitAllowingStateLoss();
            } catch (Exception e) {
                e.printStackTrace();
            }
            view.findViewById(layoutID).setVisibility(View.VISIBLE);
        }
    }

    public void removeFragment(BaseFragment fragment) {
        try {
            FragmentTransaction fragmentTransaction = getChildFragmentManager()
                    .beginTransaction();
            fragmentTransaction.remove(fragment).commitAllowingStateLoss();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void replaceFragment(int layoutID, BaseFragment baseFragment) {
        if (baseFragment == null)
            view.findViewById(layoutID).setVisibility(View.GONE);
        else {
            try {
                FragmentTransaction fragmentTransaction = getFragmentManager()
                        .beginTransaction();
                BaseFragment fragmentExistInStack = (BaseFragment) getFragmentManager().findFragmentByTag(baseFragment.getClass().toString());
                if (fragmentExistInStack != null) {
                    fragmentTransaction.replace(layoutID, fragmentExistInStack);
                } else
                    try {
                        fragmentTransaction.add(layoutID, baseFragment, baseFragment
                                .getClass().toString());
                    } catch (Exception e) {
                    }
                fragmentTransaction.setCustomAnimations(android.R.anim.slide_in_left,
                        android.R.anim.slide_out_right);
                fragmentTransaction.addToBackStack(null);
                fragmentTransaction.commitAllowingStateLoss();
            } catch (Exception e) {
                e.printStackTrace();
            }
            view.findViewById(layoutID).setVisibility(View.VISIBLE);
        }
    }

}
